#!/bin/bash -xev

if [ $# != 2 ]; then
	echo "Usage: $0 PKG_PATH Description"
	exit
fi

WEPS_DIR="var/www/weps/repository/GeoAmida-Gamma/System Software/"
rm -rf var/
mkdir -p "$WEPS_DIR"
j=1
TOTAL_PKGS=`ls $1|wc -l`
(
for i in `ls $1/*.ppp`
	do
		echo "$[$j*100/$TOTAL_PKGS]"
		j=$[$j+1]

		BASENAME=`basename $i`
		echo "# $BASENAME"
		VERSION=`echo $BASENAME|rev|cut -f2 -d\-|rev`
		PACKAGE_NAME=`echo $BASENAME | awk -F "-" '{ pkg=$1; for(f=2; f<=NF-2; f++) { pkg=pkg"-"$f } ; ver=NF-1; printf("%s\n", pkg);}'`
		ARC_ID=46
		DESCRIPTION=$2
		PAC_ID=`echo "select id from package where cat_id=69 and name='$PACKAGE_NAME'"|mysql -uweps -hterminator weps -s`
		if [ ! $PAC_ID ]; then
			echo "# $BASENAME updating weps"
			echo "INSERT INTO package (name,description,cat_id) VALUES ('$PACKAGE_NAME','$PACKAGE_NAME',69)"|mysql -uweps -hterminator weps -s
			DESCRIPTION="Initial Version"
			PAC_ID=`echo "select id from package where cat_id=69 and name='$PACKAGE_NAME'"|mysql -uweps -hterminator weps -s`
		fi
		TYP_ID=1
		CURR_VER=`echo "select version from file where id IN (select max(id) from file where pac_id=$PAC_ID)" | mysql -uweps -hterminator weps -s`
		IS_VERSION=`echo "select version from file where id IN (select id from file where pac_id=$PAC_ID) and version='$VERSION'" | mysql -uweps -hterminator weps -s`
		#echo "file: $i , weps latest version:$CURR_VER"
		if [ "$IS_VERSION" != $VERSION ]; then
			echo "INSERT INTO file (name,version,arc_id,pac_id,typ_id,description) VALUES ('$BASENAME','$VERSION',$ARC_ID,$PAC_ID,$TYP_ID,'$DESCRIPTION')"|mysql -uweps -hterminator weps -s
			mkdir -p "$WEPS_DIR/$PACKAGE_NAME"
			cp "$1/$BASENAME" "$WEPS_DIR/$PACKAGE_NAME/"
			sudo chown www-data.www-data "$WEPS_DIR/$PACKAGE_NAME/$BASENAME"		
		fi

	done

WEPS_DIR="var/www/weps/repository/GeoAmida-Gamma/Development Libraries"
mkdir -p "$WEPS_DIR"

for i in `ls $1/*.tar.bz2`
	do
		echo "$[$j*100/$TOTAL_PKGS]"
		j=$[$j+1]

		BASENAME=`basename $i`
		echo "# $BASENAME"
		VERSION=`echo $BASENAME|rev|cut -f2 -d\-|rev`
		PACKAGE_NAME=`echo $BASENAME | awk -F "-" '{ pkg=$1; for(f=2; f<=NF-2; f++) { pkg=pkg"-"$f } ; ver=NF-1; printf("%s\n", pkg);}'`
		ARC_ID=46
		DESCRIPTION=$2
		PAC_ID=`echo "select id from package where cat_id=72 and name='$PACKAGE_NAME'"|mysql -uweps -hterminator weps -s`
		if [ ! $PAC_ID ]; then
			echo "# $BASENAME updating weps"
			echo "INSERT INTO package (name,description,cat_id) VALUES ('$PACKAGE_NAME','$PACKAGE_NAME',72)"|mysql -uweps -hterminator weps -s
			DESCRIPTION="Initial Version"
			PAC_ID=`echo "select id from package where cat_id=72 and name='$PACKAGE_NAME'"|mysql -uweps -hterminator weps -s`
		fi
		TYP_ID=1
		CURR_VER=`echo "select version from file where id IN (select max(id) from file where pac_id=$PAC_ID)" | mysql -uweps -hterminator weps -s`
		IS_VERSION=`echo "select version from file where id IN (select id from file where pac_id=$PAC_ID) and version='$VERSION'" | mysql -uweps -hterminator weps -s`
		#echo "file: $i , weps latest version:$CURR_VER"
		if [ "$IS_VERSION" != $VERSION ]; then
			echo "INSERT INTO file (name,version,arc_id,pac_id,typ_id,description) VALUES ('$BASENAME','$VERSION',$ARC_ID,$PAC_ID,$TYP_ID,'$DESCRIPTION')"|mysql -uweps -hterminator weps -s
			mkdir -p "$WEPS_DIR/$PACKAGE_NAME"
			cp "$1/$BASENAME" "$WEPS_DIR/$PACKAGE_NAME/"
			sudo chown www-data.www-data "$WEPS_DIR/$PACKAGE_NAME/$BASENAME"		
		fi

	done
	)|zenity --progress \
	--auto-close \
	--title="Update progress" \
	--text="Weps updating" \
	--percentage=0

echo "Done"

zenity --info \
       --text="Weps Updation compleated"


